﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using EmoneyWeiXinCommon;
using EmoneyWeiXinEntity;

namespace EmoneyWeiXinThreadDAL
{
    public class weixinUserDal
    {
        public List<weixinUserEntity> getWeixinUserAll()
        {
            string sql = "select id, title, Author, AppId, AppSecret, Token, EncodingAESKey, servicesUrl, gzUrl, IsMicrocommunity, MicrocommunityUrl, createtime, updatetime, isvalid, remark, UpdateUser, createUser from weixinUser with(nolock) where isvalid=1";
            return SQLHelper.GetDataByColumnsLst<weixinUserEntity>(Config.EmoneyWeiXin, sql, null);
        }

        public List<MenuEntity> getMemuAll()
        {
            string sql = "SELECT * FROM dbo.menu with(nolock) WHERE isvalid=1 AND isThread=1 AND starttime<GETDATE() AND endtime>GETDATE()";
            return SQLHelper.GetDataByColumnsLst<MenuEntity>(Config.EmoneyWeiXin, sql, null);
        }

        public List<MenuEntity> getMemuAll1()
        {
            string sql = "SELECT * FROM dbo.menu with(nolock) WHERE isvalid=1";
            return SQLHelper.GetDataByColumnsLst<MenuEntity>(Config.EmoneyWeiXin, sql, null);
        }


   


        public int getStockDoctorlog2Update(StockDoctorlogEntity stockDoctor)
        {
            try
            {
                SQLTools tools = new SQLTools();
                tools.AddSqlParameter("weixinid", SqlDbType.Int, 50, stockDoctor.weixinid);
                tools.AddSqlParameter("newsreportid", SqlDbType.VarChar, 10000000, stockDoctor.newsreportid);
                tools.AddSqlParameter("FromUserName", SqlDbType.NVarChar, 200, stockDoctor.FromUserName);
                tools.AddSqlParameter("updatetime", SqlDbType.DateTime, 500, stockDoctor.updatetime);

                string sql = "insert into StockDoctorlog2(weixinid, newsreportid, FromUserName, updatetime) values("
                    + "@weixinid,"
                    + "@newsreportid,"
                    + "@FromUserName,"
                    + "@updatetime"
                    + ")";
                int aa = SQLHelper.ExecuteNonQuery(Config.EmoneyWeiXin, System.Data.CommandType.Text, sql, tools.GetSqlParameters());
                if (aa > 0)
                {

                }
                return aa;
            }
            catch (Exception ex)
            {
                StringTools.GetSendMail("getStockDoctorlogUpdate:error:" + ex.Message);
            }
            return 0;
        }

        public int getStockDoctorlogUpdate(StockDoctorlogEntity stockDoctor)
        {
            try
            {
                SQLTools tools = new SQLTools();
                tools.AddSqlParameter("weixinid", SqlDbType.Int, 50, stockDoctor.weixinid);
                tools.AddSqlParameter("newsreportid", SqlDbType.VarChar, 10000000, stockDoctor.newsreportid);
                tools.AddSqlParameter("FromUserName", SqlDbType.NVarChar, 200, stockDoctor.FromUserName);
                tools.AddSqlParameter("wType", SqlDbType.Int, 5, stockDoctor.wType);
                tools.AddSqlParameter("ToUserName", SqlDbType.NVarChar, 200, stockDoctor.ToUserName);
                tools.AddSqlParameter("CONTENT", SqlDbType.NVarChar, 500000, stockDoctor.CONTENT);
                tools.AddSqlParameter("PicUrl", SqlDbType.NVarChar, 5000, stockDoctor.PicUrl);
                tools.AddSqlParameter("eventkey", SqlDbType.NVarChar, 500, stockDoctor.eventkey);
                tools.AddSqlParameter("Stock", SqlDbType.VarChar, 500, stockDoctor.Stock);
                tools.AddSqlParameter("isStock", SqlDbType.NVarChar, 5000, stockDoctor.isStock);
                tools.AddSqlParameter("result", SqlDbType.NVarChar, 50000, stockDoctor.result);
                tools.AddSqlParameter("updatetime", SqlDbType.DateTime, 500, stockDoctor.updatetime);

                string sql = "insert into StockDoctorlog(weixinid, newsreportid, FromUserName, wType, ToUserName,CONTENT, PicUrl, eventkey, Stock, isStock, isreply, result,Updatetime) values("
                    + "@weixinid,"
                    + "@newsreportid,"
                    + "@FromUserName,"
                    + "@wType,"
                    + "@ToUserName,"
                    + "@CONTENT,"
                    + "@PicUrl,"
                    + "@eventkey,"
                    + "@Stock,@isStock,0,"
                    + "@result,"
                    + "@updatetime"
                    + ")";
                int aa = SQLHelper.ExecuteNonQuery(Config.EmoneyWeiXin, System.Data.CommandType.Text, sql, tools.GetSqlParameters());
                return aa;
            }
            catch (Exception ex)
            {
                StringTools.GetSendMail("getStockDoctorlogUpdate:error:" + ex.Message);
            }
            return 0;
        }


        public List<StockDoctorloguserEntity> getStockdoctorUser(string weixinid, string content, string MemuType)
        {
            string sql = "SELECT DISTINCT(FromUserName) FROM ( ";
            sql += " SELECT DISTINCT(FromUserName) FROM StockDoctorlog_wtype1 WITH(NOLOCK)  ";
            sql += " WHERE weixinid=" + weixinid + " AND wtype=1 AND eventkey='" + MemuType + "'  ";
            sql += " UNION ALL ";
            sql += " SELECT DISTINCT(FromUserName) FROM StockDoctorlog_wtype110 WITH(NOLOCK)  ";
            sql += " WHERE weixinid=" + weixinid + " AND wtype=1 ";
            if (!content.Contains(","))
            {
                sql += " AND CONTENT IN ('" + content + " ',' " + content + "',' " + content + " ','0" + content + "','" + content + "') ";
            }
            else
            {
                sql += " AND CONTENT IN (";
                string[] strArr = content.Split(',');
                for (int i = 0; i < strArr.Length; i++)
                {
                    sql += "'" + content + " ',' " + content + "',' " + content + " ','0" + content + "','" + content + "'";
                    if (i != strArr.Length - 1)
                    {
                        sql += ",";
                    }
                }
                sql += ")";
            }
            sql += " ) t1 INNER JOIN WXUser_Group T2 WITH(NOLOCK) ON t1.FromUserName=t2.openid AND t2.weixinid=" + weixinid + " AND t2.groupUserid=17";
            return SQLHelper.GetDataByColumnsLst<StockDoctorloguserEntity>(Config.EmoneyWeiXin, sql, null);
        }

        public int getPropellingUpdate(PropellingLogEntity porpeEn)
        {
            string sql = "";
            if (porpeEn.id > 0)
            {
                sql = " update PropellingLog set weixinid='" + porpeEn.weixinid + "',newsreportid='" + porpeEn.newsreportid + "',ToUsername='" + porpeEn.ToUsername + "',errorResult='" + porpeEn.errorResult + "',updatetime='" + porpeEn.updatetime + "' where id=" + porpeEn.id;
            }
            else
            {
                sql = " insert into PropellingLog(weixinid, newsreportid, ToUsername, errorResult, updatetime,hisid) values('" + porpeEn.weixinid + "','" + porpeEn.newsreportid + "','" + porpeEn.ToUsername + "','" + porpeEn.errorResult + "','" + porpeEn.updatetime + "','" + porpeEn.hisid + "')";
            }
            return SQLHelper.ExecuteNonQuery(Config.EmoneyWeiXin, System.Data.CommandType.Text, sql, null);
        }
    }
}
